<?php

namespace app\system\controller;
use app\common\base\_Db;
use app\common\controller\CommonController;
use app\common\entity\RequestEntity;
use think\Db;

class Department extends CommonController{
    /**
     * 添加部门
     */
    public function AddDepartment(){
        $DepartmentName = new RequestEntity('DepartName', '部门名称', false, 32);
        $DepartmentFid = new RequestEntity('DepartFid', '上级部门id', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        if(
            Db::name('department')
            ->insert(
                [
                    'depart_name'=>$DepartmentName->text,
                    'depart_pid'=>$DepartmentFid->text,
                    'createtime'=>date('Y-m-d H:i:s',time()),
                    'oper_id'=>$post_creatid->text
                ]
            )
        ){
            _Db::log($post_creatid->text,1,'部门添加');
            $this->ListDepartment();
        }else{
            _Db::Response(0,'添加失败');
        }
    }
    /**
     * 部门列表
     */
    public function ListDepartment(){
        $param_page = $get_page = input('Page', 1);
        $param_pagesize =  10;
        $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0);
        $where = '1=1';
        if(!empty($get_keyword->text)){
            $where .= ' and depart_name  like "' . $get_keyword->text.'%"';
        }
        $res = Db::name('department')
            ->field([
                'depart_id'=>'DepartId',
                'depart_name'=>'DepartName',
                'depart_pid'=>'Fid',
                'oper_id'=>'Operator',
                'createtime'=>'Time',
            ])
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->select();
        foreach ($res as $key => $value){
            $res[$key]['FName'] = $this->GetFDepart($value['Fid']);
        }
        foreach ( $res as $key => $value){
            if($res[$key]['Operator']){//操作者id 转换姓名
                $res[$key]['Operator']=$this->GetUserName($res[$key]['Operator']);
            }
            $res[$key]['Order'] = $key + 1;
        }
        $int_total_count = count(Db::name('department')->where($where)->select());
        $data['tableData']=$res;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    public function GetFDepart($id){
        $res = Db::name('department')
            ->field('depart_name')
            ->where('depart_id ='.$id)
            ->find();
        if($res['depart_name'] == null){
            $res['depart_name'] = '无';
        }
        return $res['depart_name'];
    }
    public function GetUserName($id){
        $res = Db::name('staff')
            ->field('nick_name')
            ->where('staff_id ='.$id)
            ->find();
        return $res['nick_name'];
    }
    public function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){
          $tree=array();
          $packData=array();
         foreach ($list as $data) {
               //转换为带有主键id的数组
            $packData[$data[$pk]] = $data; //$packData[1]=$data; $packData[2]=$data 
        }
        foreach ($packData as $key =>$val){
            if($val[$pid]==$root){   //代表跟节点       
                 $tree[]=& $packData[$key];
            }else{
           //找到其父类
            $packData[$val[$pid]][$child][]=& $packData[$key];
            }
        }
        return $tree;
    }
    /**
     * 部门详情
     */
    public function ItemDepartment(){
        $DepartmentId = new RequestEntity('DepartId', '部门id', false, 32);
        $res = Db::name('department')
            ->field([
                'depart_id',
                'depart_name',
                'depart_pid',
                'createtime'
            ])
            ->where('depart_id ='.$DepartmentId->text)
            ->find();
        _Db::ResponseJson($res);
    }
    public function FList(){
        $DepartmentId = new RequestEntity('DepartId', '部门id', false, 32);
        if ($DepartmentId->text){
            $where = 'depart_id != '.$DepartmentId->text;
        }else{
            $where = '1=1';
        }
        $res = Db::name('department')
            ->field([
                'depart_name',
                'depart_id',
            ])
            ->where($where)
            ->select();
        $count = count($res);
        $res[$count] = [
            'depart_id'=>0,
            'depart_name'=>'无'
        ];
        return json_encode($res);
    }
    /**
     * 部门修改
     */
    public function ModifyDepartment(){
        $DepartmentId = new RequestEntity('DepartId', '部门id', false, 32);
        $DepartmentFid = new RequestEntity('DepartFid', '部门父id', false, 32);
        $DepartmentName = new RequestEntity('DepartName', '部门名称', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        if(
            Db::name('department')
                ->where('depart_id ='.$DepartmentId->text)
            ->update([
                'depart_name'=>$DepartmentName->text,
                'depart_pid'=>$DepartmentFid->text,
                'createtime'=>date('Y-m-d H:i:s',time()),
            ])
        ){
            _Db::log($post_creatid->text,3,'部门修改');
            $this->Item($DepartmentId->text);
        }else{
            _Db::Response(0,'修改失败');
        }
    }
    public function Item($id){
        $res = Db::name('department')
            ->field([
                'depart_id',
                'depart_name',
                'depart_pid',
                'createtime'
            ])
            ->where('depart_id ='.$id)
            ->find();
        $res['FName'] = $this->GetFDepart($res['depart_pid']);
        _Db::ResponseJson($res);
    }
    /**
     * 部门删除
     */
    public function RemoveDepartment(){
        $DepartmentId = new RequestEntity('DepartId', '部门id', false, 32);
        $post_creatid = new RequestEntity('CreatId', '操作人', false, 32);
        $res = Db::name('department')
            ->field('depart_name')
            ->where('depart_pid ='.$DepartmentId->text)
            ->select();
        $res_staff = Db::name('staff')
            ->field('nick_name')
            ->where('department_id ='.$DepartmentId->text)
            ->select();
        if(!empty($res)){
            _Db::Response(3,'有下级部门不能删除');
        }elseif (!empty($res_staff)){
            _Db::Response(3,'该部门下存在员工不能删除');
        }
        else{
            Db::name('department')
             ->where('depart_id ='.$DepartmentId->text)
             ->delete();
            _Db::log($post_creatid->text,2,'部门删除');
            $this->ListDepartment();
        }
    }
}